#include "librtsp.h"

RtspClient *ConectRtsp(char *ip,int port,char *desc){
    if(0 == ip || 0 == desc){
        return 0;
    }
    RtspClient *ret = (RtspClient*)malloc(sizeof(RtspClient));
    ret->sock = Connect(ip,port);
    if(ret->sock < 0){
        return 0;
    }
    strcpy(ret->hostIp,ip);
    ret->port = port;
    ret->cesq = 0;
    strcpy(ret->despcrtion,desc);
    return ret;
}

int SendOption(RtspClient*p,CallBack *c){
    char pRecv[500];
    char buf[500];
    sprintf(buf,"OPTIONS rtsp://%s:%d/%s RTSP/1.0\r\nCSeq: %d\r\nUser-Agent: Lavf57.71.100\r\n\r\n",p->hostIp,p->port,
            p->despcrtion,p->cesq);
    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        closesocket(p->sock);
        p->conn = FALSE;
        return -1;
    }
    if(Recv(p->sock,pRecv,500) > 0) {
        printf("recv: %s\r\n",pRecv);
    } else{
        printf("error recv ");
        return -1;
    }
}

int SendAnnounce(RtspClient *p,CallBack callback){
    char pRecv[500] = {0};
    char buf[500] = {0};
    char length[10] = {0};
    sprintf(buf,"ANNOUNCE rtsp://%s:%d/%s RTSP/1.0\r\nContent-Type: application/sdp\r\n"
                "CSeq: %d\r\nUser-Agent: Lavf57.71.100\r\nContent-Length:",p->hostIp,p->port,
            p->despcrtion,p->cesq);
    sprintf(length,"%d",strlen(buf));
    int len = strlen(buf) + 7  +  strlen(length);
    sprintf(length,"%d",len);
    strcat(buf,length);
    strcat(buf,"\r\n\r\n");

    int ret = Send(p->sock,buf,strlen(buf));
    printf("send %d \r\ndata is:\r\n%s\r\n",ret,buf);
    if(0 > ret){
        closesocket(p->sock);
        p->conn = FALSE;
        return -1;
    }
    if(Recv(p->sock,pRecv,500) > 0) {
        printf("recv: %s\r\n",pRecv);
    } else{
        printf("error recv ");
        return -1;
    }
}